<template>
    <div class="m_right">
        <p></p>
        <div class="mem_tit">
            <span class="fr"
                style="font-size:12px; color:#555555; font-family:'宋体'; margin-top:5px;">共发现{{ collectList.length }}件</span>我的收藏
        </div>
        <table border="0" class="order_tab" style="width:930px;" cellspacing="0" cellpadding="0"
            v-if="collectList.length != 0 && collectList != null">
            <tr>
                <td align="center" width="420">商品名称</td>
                <td align="center" width="180">价格</td>
                <td align="center" width="270">操作</td>
            </tr>
            <tr v-for="collect in collectList" :key="collect.id">
                <td style="font-family:'宋体';">
                    <div class="sm_img"><img
                            :src="'http://192.168.144.183:9090/easybuyImages/' + collect.product.fileName" width="48"
                            height="48" /></div>
                    {{ collect.product.name }}
                </td>
                <td align="center">￥{{ collect.product.price }}</td>
                <td align="center">
                    <a href="#" style="color:#ff4e00;"
                        @click.prevent="getBuyCar(collect.product)">加入购物车</a>&nbsp; &nbsp;
                    <a href="#" @click.prevent="deleteCollect(collect.id)">删除</a>
                </td>
            </tr>
        </table>
        <div v-if="collectList.length == 0 || collectList == null">
            <span>暂无收藏数据。。。</span>
        </div>
    </div>
</template>
<script>
export default {
    name: 'MemberCollectRight',
    data() {
        return {
            userId: '',
            collectList: [],
            buyCar: null,
        }
    },
    methods: {
        getAllCollect() {
            this.$axios({
                url: 'getCollectList',
                method: 'get',
                params: { userId: this.userId }
            }).then(rs => {
                if (rs.data.code == '5200' || rs.data.code == 5200) {
                    this.collectList = rs.data.collects
                }
            })
        },
        deleteCollect(id) {
            if (confirm('是否删除？')) {
                this.$axios({
                    url: 'deleteCollect',
                    method: 'get',
                    params: { id: id, userId: this.userId }
                }).then(rs => {
                    if (rs.data.code == 5200 || rs.data.code == '5200') {
                        alert('删除成功！')
                        this.getAllCollect()
                        
                    } else {
                        alert('删除失败！')
                    }
                })
            }
        },
        addToBuyCar(product, pid) {
            if (this.buyCar == null || this.buyCar == '') {
                this.$axios({
                    url: 'addBuyCar',
                    method: 'post',
                    data: {
                        userId: this.userId,
                        number: 1,
                        product: product,
                        pid: pid
                    },
                    headers: {
                        'content-type': 'application/json'
                    }
                }).then(rs => {
                    if (rs.data.code == 5200 || rs.data.code == '5200') {
                        alert('添加成功！')
                    } else {
                        alert('添加失败！')
                    }
                })
            }else{
                console.log(this.buyCar.product.stock)
                if(this.buyCar.number + 1 > product.stock){
                    alert('库存不足！')
                    return
                }

                this.$axios({
                    url:'updateBuyCar',
                    method:'post',
                    data:{
                        id:this.buyCar.id,
                        number:this.buyCar.number+1
                    },
                    headers:{'content-type':'application/json'}
                }).then(rs=>{
                    if(rs.data.code == 5200 || rs.data.code == '5200'){
                        alert('添加成功！')
                    }else{
                        alert('添加失败！')
                    }
                })
            }

        },
        
        getBuyCar(product) {
            this.$axios({
                url: 'addBuyCarNum',
                method: 'get',
                params: { userId: this.userId, pid: product.id }
            }).then(rs => {
                if (rs.data.code == 5200 || rs.data.code == '5200') {
                    this.buyCar = rs.data.buyCar
                    this.addToBuyCar(product, product.id)
                }else{
                    alert('添加失败！')
                }
            })
        }
    },
    mounted() {
        this.userId = window.sessionStorage.getItem('userId')
        this.getAllCollect()
    }
}
</script>